package leetcodecompetition.D374;

import org.junit.jupiter.api.Test;

/**
 * @Author: Siantar
 * @Date: 2023-12-03-11:06
 * @Description: 1.0
 */
public class T100145 {
    public int countCompleteSubstrings(String word, int k) {
        int n = word.length();
        int res = 0;
        int p = 0;
        for (int i = 1; i < word.length(); i++) {
            if (Math.abs(word.charAt(i) - word.charAt(i - 1)) > 2) {
                res += sum(word, k, p, i - 1);
                p = i;
            }
        }
        res += sum(word, k, p, word.length() - 1);
        return res;
    }

    public int sum(String word, int k, int from, int to) {
        System.out.println(to);
        int res = 0;
        for (int i = k; i <= to - from + 1; i += k) {
            if (i / k > 26) break;
            System.out.println("k:" + i);
            int[] sum = new int[26];
            for (int j = from; j < from + i; j++) {
                sum[word.charAt(j) - 'a']++;
            }
            if (is(sum, k)) res++;
            for (int j = from + i; j <= to; j++) {
                sum[word.charAt(j) - 'a']++;
                sum[word.charAt(j - i) - 'a']--;
                if (is(sum, k)) res++;
            }
        }
        return res;
    }

    public boolean is(int[] sum, int k) {
        for (int num : sum) {
            if (num != 0 && num != k) return false;
        }
        return true;
    }

    @Test
    public void test() {
        System.out.println(countCompleteSubstrings("aaabbbccc", 3));
    }


}
